home *** CD-ROM | disk | FTP | other *** search
- package horst;
-
- public class BusyFlag {
- protected Thread busyflag;
- protected int busycount;
-
- public synchronized void freeBusyFlag() {
- if (this.getBusyFlagOwner() == Thread.currentThread()) {
- --this.busycount;
- if (this.busycount == 0) {
- Utilities.debugOut("freeBusyFlag:" + Thread.currentThread());
- this.busyflag = null;
- String msg = "BusyFlag busy freeing flag " + Thread.currentThread();
- Utilities.debugOut(msg);
- this.notify();
- Utilities.debugOut("After notify");
- }
- }
-
- }
-
- public synchronized void getBusyFlag() {
- while(!this.tryGetBusyFlag()) {
- try {
- Utilities.debugOut("Thread waiting:" + Thread.currentThread());
- this.wait();
- Utilities.debugOut("after waiting:" + Thread.currentThread());
- Utilities.debugOut("after wait");
- } catch (Exception e) {
- System.out.println("Thread:" + Thread.currentThread());
- System.out.println("getBusyFlag exception:" + ((Throwable)e).getMessage());
- System.out.println("busyflag:" + this.busyflag);
- System.out.println("tryGetBusyFlag()=" + this.tryGetBusyFlag());
- }
- }
-
- }
-
- public synchronized Thread getBusyFlagOwner() {
- return this.busyflag;
- }
-
- public synchronized boolean tryGetBusyFlag() {
- if (this.busyflag == null) {
- this.busyflag = Thread.currentThread();
- this.busycount = 1;
- Utilities.debugOut("tryGetBusyFlag:" + Thread.currentThread());
- return true;
- } else if (this.busyflag == Thread.currentThread()) {
- ++this.busycount;
- Utilities.debugOut("tryGetBusyFlag:" + Thread.currentThread());
- return true;
- } else {
- return false;
- }
- }
- }
-